<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_users
 *
 * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

/**
 * Users master display controller.
 *
 * @package     Joomla.Administrator
 * @subpackage  com_users
 * @since       1.6
 */
class ProfilesController extends JControllerLegacy
{
	/**
	 * Checks whether a user can see this view.
	 *
	 * @param   string	$view	The view name.
	 *
	 * @return  boolean
	 * @since   1.6
	 */
	protected $default_view = 'user';
	protected function canView($view)
	{
		$canDo	= UsersHelper::getActions();

		switch ($view)
		{
			// Special permissions.
			case 'groups':
			case 'group':
			case 'levels':
			case 'level':
				return $canDo->get('core.admin');
				break;

			// Default permissions.
			default:
				return true;
		}
	}

	/**
	 * Method to display a view.
	 *
	 * @param   boolean			If true, the view output will be cached
	 * @param   array  An array of safe url parameters and their variable types, for valid values see {@link JFilterInput::clean()}.
	 *
	 * @return  JController		This object to support chaining.
	 * @since   1.5
	 */
	public function display($cachable = false, $urlparams = false)
	{
		$view   = $this->input->get('view', 'users');
		$layout = $this->input->get('layout', 'default');
		$id     = $this->input->getInt('id');

		if (!$this->canView($view))
		{
			JError::raiseWarning(404, JText::_('JERROR_ALERTNOAUTHOR'));

			return;
		}

		// Check for edit form.
		if ($view == 'user' && $layout == 'edit' && !$this->checkEditId('com_profiles.edit.user', $id))
		{
			// Somehow the person just went to the form - we don't allow that.
			$this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_UNHELD_ID', $id));
			$this->setMessage($this->getError(), 'error');
			$this->setRedirect(JRoute::_('index.php?option=com_profiles&view=users', false));

			return false;
		}
		elseif ($view == 'group' && $layout == 'edit' && !$this->checkEditId('com_profiles.edit.group', $id))
		{
			// Somehow the person just went to the form - we don't allow that.
			$this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_UNHELD_ID', $id));
			$this->setMessage($this->getError(), 'error');
			$this->setRedirect(JRoute::_('index.php?option=com_profiles&view=groups', false));

			return false;
		}
		elseif ($view == 'level' && $layout == 'edit' && !$this->checkEditId('com_profiles.edit.level', $id))
		{
			// Somehow the person just went to the form - we don't allow that.
			$this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_UNHELD_ID', $id));
			$this->setMessage($this->getError(), 'error');
			$this->setRedirect(JRoute::_('index.php?option=com_profiles&view=levels', false));

			return false;
		}
		elseif ($view == 'note' && $layout == 'edit' && !$this->checkEditId('com_profiles.edit.note', $id))
		{
			// Somehow the person just went to the form - we don't allow that.
			$this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_UNHELD_ID', $id));
			$this->setMessage($this->getError(), 'error');
			$this->setRedirect(JRoute::_('index.php?option=com_profiles&view=notes', false));

			return false;
		}

		return parent::display();
	}
	
	function savePolicy(){
		$user   = & JFactory::getUser();
	    $uid  	= $user->get('id');
		// Get a db connection.
		$db = JFactory::getDbo();
		$policy = $_POST["desc"];
		$status = $_POST["status"];
		// Create a new query object.
		$query = $db->getQuery(true);
		
		$query = "update  #__user_profiles set policy_client = '$policy',policy_status = '$status' where user_id = $uid";
		$db->setQuery($query);
		$db->execute();
		$str = "index.php?option=com_profiles&view=policy";
		header("Location:$str");
	}
	
	function saveHomepage(){
		$user   = & JFactory::getUser();
	    $uid  	= $user->get('id');
		$data   = $this->input->post->get('jform', array(), 'array');
		// Get a db connection.
		$db = JFactory::getDbo();
		$hours             = $_POST["desc_hours"];
		$wellcome          = $_POST["desc_wellcome"];
		$google  	        = $data['follow']['google'];
		$facebook  	      = $data['follow']['facebook'];
		$twitter 	       = $data['follow']['twitter'];
		$imageurl 	      = $data['params']['imageurl'];
		
		if (isset($data['time']) && is_array($data['time']))
		{
			$registry = new JRegistry;
			$registry->loadArray($data['time']);
			$data['time'] = (string) $registry;
		}
		
		$time = $data['time'];
		// Create a new query object.
		$query = $db->getQuery(true);
		
		$query = "update  #__user_profiles set hours = '$hours',wellcome = '$wellcome', wellcome_img = '$imageurl',
							 google='$google',facebook='$facebook',twitter='$twitter',times='$time' where user_id = $uid";
		$db->setQuery($query);
		$db->execute();
		$str = "index.php?option=com_profiles&view=homepage";
		header("Location:$str");
	}
	
	function saveProfile(){
		$data  = $this->input->post->get('jform', array(), 'array');
		$user   = & JFactory::getUser();
	    $uid  	= $user->get('id');
		// Get a db connection.
		$db = JFactory::getDbo();
		// Create a new query object.
		$query = $db->getQuery(true);
		/* domain fields*/
		     $domain_register  	     = $data['domain']['domain_register'];
			 $domain_completed  	    = $data['domain']['domain_completed'];
			 $domain_created  	      = $data['domain']['domain_created'];
			 $domain_email  	    	= $data['domain']['domain_email'];
			 $domain_name  	    	 = $data['domain']['domain_name'];
			 $domain_purchase  	     = $data['domain']['domain_purchase'];
			 $domain_exprires  	     = $data['domain']['domain_exprires'];
			 $domain_manager  	 	  = $data['domain']['domain_manager'];
			 $domain_username  		 = $data['domain']['domain_username'];
			 $domain_password  		 = $data['domain']['domain_password'];
			 /*contact fields*/
			 $contact_name  	        = $data['contact']['contact_name'];
			 $contact_email  	       = $data['contact']['contact_email'];
			 $contact_address  	     = $data['contact']['contact_address'];
			 $contact_city 	        = $data['contact']['contact_city'];
			 $contact_zip  	    	 = $data['contact']['contact_zip'];
			 $contact_state  	       = $data['contact']['contact_state'];
			 $contact_phone  	       = $data['contact']['contact_phone'];
			 $contact_fax  	     	 = $data['contact']['contact_fax'];
			 $googleapi  	 	 	   = $data['contact']['googleapi'];
			 $siteurl  		 		 = $data['contact']['siteurl'];
			 /*follow fields*/
			 $google  	        = $data['follow']['google'];
			 $facebook  	      = $data['follow']['facebook'];
			 $twitter 	       = $data['follow']['twitter'];
			 $keyword  	       = $data['follow']['meta_keywork'];
			 $description  	   = $data['follow']['meta_desc'];
			 $title  	         = $data['follow']['meta_title'];
			 
			 $query = "update  #__user_profiles 
							  set domain_register='$domain_register',domain_completed = '$domain_completed',
							      domain_created ='$domain_created',domain_email='$domain_email',domain_name='$domain_name',
								  domain_purchase='$domain_purchase',domain_exprires='$domain_exprires',
								  domain_manager='$domain_manager',domain_username='$domain_username',domain_password='$domain_password',
								  contact_name='$contact_name',contact_email='$contact_email',contact_address='$contact_address',
								  contact_city='$contact_city',contact_zip='$contact_zip',contact_state='$contact_state',
								  contact_phone='$contact_phone',contact_fax='$contact_fax',google_api='$googleapi',site_url='$siteurl',
								  google='$google',facebook='$facebook',twitter='$twitter',seo_title='$title',seo_meta_keyword='$keyword',
								  seo_meta_description='$description'
								  where user_id = $uid";
		$db->setQuery($query);
		$db->execute();
		$str = "index.php?option=com_profiles&view=profile";
		header("Location:$str");
	}
	
	function postGallery(){
		$user   = & JFactory::getUser();
	    $uid  	= $user->get('id');
		$data   = $this->input->post->get('jform', array(), 'array');
		$id     = $_POST["id"];
		// Get a db connection.
		$db = JFactory::getDbo();
		$detail          = $_POST["detail"];
		$name          	= $_POST["name"];
		$status          = $_POST["status"];
		$imageurl 	    = $data['params']['imageurl'];
		$day			 = date("Y-m-d");
		// Create a new query object.
		$query = $db->getQuery(true);
		if(isset($id) && $id!=0 && is_numeric($id)){
				$query = "update  #__gallery set name = '$name',detail = '$detail', images = '$imageurl',
					                             status='$status',modify='$day', user_id = $uid where id = $id";
		}else{
				$query = "insert into  #__gallery(name,detail,images,status,modify,user_id) 
						                  values('$name','$detail','$imageurl','$status','$day','$uid')";
		}
		
		$db->setQuery($query);
		$db->execute();
		$str = "index.php?option=com_profiles&view=galleries";
		header("Location:$str");
	}
	
	function delGallery(){
		$id     = $_GET["id"];
		// Get a db connection.
		$db = JFactory::getDbo();
		// Create a new query object.
		$query = $db->getQuery(true);
		$query = "delete from #__gallery where id = $id";	
		$db->setQuery($query);
		$db->execute();
		$msg = "DELETE_SUCCESS";
		$str = "index.php?option=com_profiles&view=galleries&msg=$msg";
		header("Location:$str");
	}
	function delAllGallery(){
		// Get a db connection.
		$db = JFactory::getDbo();
		// Create a new query object.
		$query = $db->getQuery(true);
		$id	= $_POST["chk"];
		for($i=0;$i<count($id);$i++)
		{
            $query = "delete from #__gallery where id =".$id[$i];
			$db->setQuery($query);
			$db->execute();
		}
		
		$msg = "DELETE_SUCCESS";
		$str = "index.php?option=com_profiles&view=galleries&msg=$msg";
		header("Location:$str");
	}
	function checkGalID($id){
		$db	= &JFactory::getDBO();
		$query = $db->getQuery(true);
		$query->select('status');
		$query->from('#__gallery');
		$query->where('id='.$id);
		$db->setQuery($query);
		$results = $db->loadRow();
		return $results;
	}
	function updateStatusGal(){
		// Get a db connection.
		$db = JFactory::getDbo();
		$id  	= $_GET["id"];
		$chkstatus = $this -> checkGalID($id);
		$status  = $chkstatus[0];
		// Create a new query object.
		$query = $db->getQuery(true);
		if($status == 1){
			$query = "update  #__gallery set status = '0' where id = $id";
		}else{
			$query = "update  #__gallery set status = '1' where id = $id";
		}
		$db->setQuery($query);
		$db->execute();
		$str = "index.php?option=com_profiles&view=galleries";
		header("Location:$str");
	}
}
